
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>D3.js-Tree实战笔记2--简单的Tree demo | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="因为业务需要折腾起了图表，《D3.js-Tree 实战笔记》系列用于记录使用该库制作 Tree 图表的一些笔记。本文介绍基本场景，以及选择 demo 的过程。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="https://godbasin.github.io/front-end-playground">前端游乐场</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2018/01/01/d3-tree-notes-2-init-a-d3-tree/" title="D3.js-Tree实战笔记2--简单的Tree demo" itemprop="url">D3.js-Tree实战笔记2--简单的Tree demo</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2018-01-01T06:11:53.000Z" itemprop="datePublished">2018-01-01</time>
    更新日期:<time datetime="2018-08-18T15:22:20.249Z" itemprop="dateModified">2018-08-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#基本场景"><span class="toc-number">1.</span> <span class="toc-text">基本场景</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Tree-图与服务"><span class="toc-number">1.1.</span> <span class="toc-text">Tree 图与服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#选择插件库"><span class="toc-number">1.2.</span> <span class="toc-text">选择插件库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#寻找-Demo"><span class="toc-number">1.3.</span> <span class="toc-text">寻找 Demo</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解读-Demo"><span class="toc-number">2.</span> <span class="toc-text">解读 Demo</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#整体逻辑"><span class="toc-number">2.1.</span> <span class="toc-text">整体逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#代码说明"><span class="toc-number">2.2.</span> <span class="toc-text">代码说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">2.3.</span> <span class="toc-text">结束语</span></a></li></ol></li></ol>
		</div>
		
		<p>因为业务需要折腾起了图表，《D3.js-Tree 实战笔记》系列用于记录使用该库制作 Tree 图表的一些笔记。本文介绍基本场景，以及选择 demo 的过程。</p>
<a id="more"></a>
<h1 id="基本场景"><a href="#基本场景" class="headerlink" title="基本场景"></a>基本场景</h1><hr>
<h2 id="Tree-图与服务"><a href="#Tree-图与服务" class="headerlink" title="Tree 图与服务"></a>Tree 图与服务</h2><p>首先我们来看个简单的图：<br><img src="https://about-position-1255459943.file.myqcloud.com/1513409805%281%29.png" alt="image"></p>
<p>没错，这就是个简单的树状图。 本骚年此次要解决的问题包括：</p>
<ol>
<li>每一层的子节点为动态请求加载。</li>
<li>子节点可以收起和展开。</li>
<li>服务的层级最多为 15 层，每层数量最多或许在 20 左右，需要考虑文字是否重叠。</li>
<li>支持鼠标滚动缩放 + 鼠标拖动。</li>
<li>节点根据服务类型展示为不同的样式。</li>
<li>鼠标放置在节点上，需要展示更多的内容。</li>
<li>连接线需要展示该链路的一些状态和信息（容量大小等）。</li>
<li>连接线根据链路当前状态，需要展示为不同颜色。</li>
</ol>
<p>以上等等。其实大致数据内容大家也大概能猜到了，主要展示服务的调用关系，以及状态信息、容灾能力等，方便整体的管理和维护。</p>
<h2 id="选择插件库"><a href="#选择插件库" class="headerlink" title="选择插件库"></a>选择插件库</h2><p>本骚年首先翻了翻 Echarts（毕竟配置化的用起来简单），很可惜 Echarts 翻来翻去都木有树状图（别翻了，本骚年完成了这个开发工作的时候，它突然又提供支持了）。</p>
<p>D3 之前也有稍微看过，对比 Echarts 的使用简单，它似乎更适合个性化或者说不是那么通用的场景（对，就是本骚年的这次需求了）。</p>
<p>关于 D3 和 Echarts 的对比，网上有人说（感觉挺有道理的）：</p>
<ul>
<li>D3 属于是基础的绘图库(基于 canvas 的, 所以可以说完全不做兼容考虑)，封装的功能都是一些基础图形图像和动画这类的，但是功能异常强大。</li>
<li>Echarts 属于应用型(或者业务型)的图表库， 期望的是： 几乎不需要写绘图过程代码就能生成漂亮的图表。</li>
</ul>
<p>决定好要使用 D3 之后，看了下，D3 最新版本为 v4，而网上很多的 demo 都是基于 v3 的，但是作为一个对自己有要求的程序员，怎么可以容忍使用旧版本呢！</p>
<p>其实真相是这样的：<br><img src="https://about-position-1255459943.file.myqcloud.com/1513412297%281%29.png" alt="image"></p>
<p>Sign~</p>
<p>而 v4 版本相对 v3 版本的变更，其实很关键的是模块化，更多的可以参考<a href="https://github.com/xswei/d3js_doc/tree/master/ReleaseNotes" target="_blank" rel="external">D3 4.0 做了哪些改进</a>。</p>
<h2 id="寻找-Demo"><a href="#寻找-Demo" class="headerlink" title="寻找 Demo"></a>寻找 Demo</h2><p>来，我们再从网上找个现有的 Demo 下手吧，就它了：<a href="https://bl.ocks.org/d3noob/43a860bc0024792f8803bba8ca0d5ecd" target="_blank" rel="external">Collapsible tree diagram in v4</a>。这里还有个 v3 版本的，大家可以对比着看一下：<a href="http://bl.ocks.org/d3noob/8375092" target="_blank" rel="external">Interactive d3.js tree diagram</a>。</p>
<p>其实 v4 和 v3 版本，不一致的地方比较关键的是<code>link</code>连接线的绘制，<code>diagonal</code>函数：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">// v3</div><div class="line">var diagonal = d3.svg.diagonal().projection(function(d) &#123;</div><div class="line">  return [d.y, d.x];</div><div class="line">&#125;);</div><div class="line"></div><div class="line">// v4</div><div class="line">function diagonal(s, d) &#123;</div><div class="line">  path = `M $&#123;s.y&#125; $&#123;s.x&#125;</div><div class="line">          C $&#123;(s.y + d.y) / 2&#125; $&#123;s.x&#125;,</div><div class="line">            $&#123;(s.y + d.y) / 2&#125; $&#123;d.x&#125;,</div><div class="line">            $&#123;d.y&#125; $&#123;d.x&#125;`;</div><div class="line">  return path;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>我们看看<a href="https://github.com/d3/d3/wiki/SVG-%E5%BD%A2%E7%8A%B6#diagonal" target="_blank" rel="external">官方文档 v3</a>的描述。</p>
<p><code>d3.svg.diagonal()</code>：使用默认的配置构造一个对角线生成器；返回值可以用来当做函数使用来生成三次贝塞尔曲线数据，该曲线的若干条切线可以保证节点连接处会有平滑的介入效果。</p>
<p>其实这里我们主要给连接线绘制路径，D3 里面主要使用了<a href="https://www.zhihu.com/question/29565629" target="_blank" rel="external">贝塞尔曲线</a>。在 v3 版本中，D3 提供了生成该曲线的方法，而在 v4 版本中，我们需要自己构造这样的曲线。</p>
<p>这里涉及到的并不是 D3 独有的，而是<code>SVG-&lt;path&gt;</code>的特性，<code>&lt;path&gt;</code>在上一节也简单说过，大家可以补充看看<a href="https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths" target="_blank" rel="external">SVG 路径 | MDN</a>，里面讲到了直线命令和曲线命令。</p>
<h1 id="解读-Demo"><a href="#解读-Demo" class="headerlink" title="解读 Demo"></a>解读 Demo</h1><p>我们先结合上一节大致讲到的 D3 基础，来解读下我们的 Demo。</p>
<hr>
<h2 id="整体逻辑"><a href="#整体逻辑" class="headerlink" title="整体逻辑"></a>整体逻辑</h2><p>我们的 Demo 实现了这样的功能：</p>
<ol>
<li>定义 svg 根元素，设置基本的属性（宽高等）。</li>
<li>定义基本的 treemap。</li>
<li>将数据转换成需要的格式。</li>
<li>将数据绑定 treemap。</li>
<li>定义子节点的选择集，以及过渡动画。</li>
<li>添加 toggle 处理，并绑定到节点。</li>
</ol>
<h2 id="代码说明"><a href="#代码说明" class="headerlink" title="代码说明"></a>代码说明</h2><p>我们结合代码来看看，其实 Demo 里面原有英文的说明，这里本骚年可能更多的是翻译的作用吧：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div></pre></td><td class="code"><pre><div class="line">// 嗯，这是最初的数据。</div><div class="line">var treeData = &#123;</div><div class="line">  name: "Top Level",</div><div class="line">  children: [</div><div class="line">    &#123;</div><div class="line">      name: "Level 2: A",</div><div class="line">      children: [&#123; name: "Son of A" &#125;, &#123; name: "Daughter of A" &#125;]</div><div class="line">    &#125;,</div><div class="line">    &#123; name: "Level 2: B" &#125;</div><div class="line">  ]</div><div class="line">&#125;;</div><div class="line"></div><div class="line">// 设置图表的宽高和Margin</div><div class="line">var margin = &#123; top: 20, right: 90, bottom: 30, left: 90 &#125;,</div><div class="line">  width = 960 - margin.left - margin.right,</div><div class="line">  height = 500 - margin.top - margin.bottom;</div><div class="line"></div><div class="line">var svg = d3</div><div class="line">  .select("body")</div><div class="line">  // 在页面的body里添加svg对象</div><div class="line">  .append("svg")</div><div class="line">  // 设置svg宽高</div><div class="line">  .attr("width", width + margin.right + margin.left)</div><div class="line">  .attr("height", height + margin.top + margin.bottom)</div><div class="line">  // 在svg里添加group元素</div><div class="line">  .append("g")</div><div class="line">  // 将group放置在左上方</div><div class="line">  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");</div><div class="line"></div><div class="line">var i = 0,</div><div class="line">  duration = 750,</div><div class="line">  root;</div><div class="line"></div><div class="line">// 定义Tree层级，并设置宽高</div><div class="line">var treemap = d3.tree().size([height, width]);</div><div class="line"></div><div class="line">// 计算父节点、字节点、高度和深度（parent, children, height, depth）</div><div class="line">root = d3.hierarchy(treeData, function(d) &#123;</div><div class="line">  return d.children;</div><div class="line">&#125;);</div><div class="line">// 设置第一个元素的初始位置</div><div class="line">root.x0 = height / 2;</div><div class="line">root.y0 = 0;</div><div class="line"></div><div class="line">// 第二层以上元素收起</div><div class="line">root.children.forEach(collapse);</div><div class="line"></div><div class="line">// 更新节点状态</div><div class="line">update(root);</div><div class="line"></div><div class="line">// collapse方法，用于切换子节点的展开和收起状态</div><div class="line">function collapse(d) &#123;</div><div class="line">  if (d.children) &#123;</div><div class="line">    d._children = d.children;</div><div class="line">    d._children.forEach(collapse);</div><div class="line">    d.children = null;</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">function update(source) &#123;</div><div class="line">  // 设置节点的x、y位置信息</div><div class="line">  var treeData = treemap(root);</div><div class="line"></div><div class="line">  // 计算新的Tree层级</div><div class="line">  var nodes = treeData.descendants(),</div><div class="line">    links = treeData.descendants().slice(1);</div><div class="line"></div><div class="line">  // 设置每个同级节点间的y间距为180</div><div class="line">  nodes.forEach(function(d) &#123;</div><div class="line">    d.y = d.depth * 180;</div><div class="line">  &#125;);</div><div class="line"></div><div class="line">  // ****************** 节点部分操作 ***************************</div><div class="line"></div><div class="line">  // 给节点添加id，用于选择集索引</div><div class="line">  var node = svg.selectAll("g.node").data(nodes, function(d) &#123;</div><div class="line">    return d.id || (d.id = ++i);</div><div class="line">  &#125;);</div><div class="line"></div><div class="line">  // 添加enter操作，添加类名为node的group元素</div><div class="line">  var nodeEnter = node</div><div class="line">    .enter()</div><div class="line">    .append("g")</div><div class="line">    .attr("class", "node")</div><div class="line">    // 默认位置为当前父节点的位置</div><div class="line">    .attr("transform", function(d) &#123;</div><div class="line">      return "translate(" + source.y0 + "," + source.x0 + ")";</div><div class="line">    &#125;)</div><div class="line">    // 给每个新加的节点绑定click事件</div><div class="line">    .on("click", click);</div><div class="line"></div><div class="line">  // 给每个新加的group元素添加cycle元素</div><div class="line">  nodeEnter</div><div class="line">    .append("circle")</div><div class="line">    .attr("class", "node")</div><div class="line">    .attr("r", 1e-6)</div><div class="line">    // 如果元素有子节点，且为收起状态，则填充浅蓝色</div><div class="line">    .style("fill", function(d) &#123;</div><div class="line">      return d._children ? "lightsteelblue" : "#fff";</div><div class="line">    &#125;);</div><div class="line"></div><div class="line">  // 给每个新加的group元素添加文字说明</div><div class="line">  nodeEnter</div><div class="line">    .append("text")</div><div class="line">    .attr("dy", ".35em")</div><div class="line">    .attr("x", function(d) &#123;</div><div class="line">      return d.children || d._children ? -13 : 13;</div><div class="line">    &#125;)</div><div class="line">    .attr("text-anchor", function(d) &#123;</div><div class="line">      return d.children || d._children ? "end" : "start";</div><div class="line">    &#125;)</div><div class="line">    .text(function(d) &#123;</div><div class="line">      return d.data.name;</div><div class="line">    &#125;);</div><div class="line"></div><div class="line">  // 获取update集</div><div class="line">  var nodeUpdate = nodeEnter.merge(node);</div><div class="line"></div><div class="line">  // 设置节点的位置变化，添加过渡动画效果</div><div class="line">  nodeUpdate</div><div class="line">    .transition()</div><div class="line">    .duration(duration)</div><div class="line">    .attr("transform", function(d) &#123;</div><div class="line">      return "translate(" + d.y + "," + d.x + ")";</div><div class="line">    &#125;);</div><div class="line"></div><div class="line">  // 更新节点的属性和样式</div><div class="line">  nodeUpdate</div><div class="line">    .select("circle.node")</div><div class="line">    .attr("r", 10)</div><div class="line">    .style("fill", function(d) &#123;</div><div class="line">      return d._children ? "lightsteelblue" : "#fff";</div><div class="line">    &#125;)</div><div class="line">    .attr("cursor", "pointer");</div><div class="line"></div><div class="line">  // 获取exit操作</div><div class="line">  var nodeExit = node</div><div class="line">    .exit()</div><div class="line">    // 添加过渡动画</div><div class="line">    .transition()</div><div class="line">    .duration(duration)</div><div class="line">    .attr("transform", function(d) &#123;</div><div class="line">      return "translate(" + source.y + "," + source.x + ")";</div><div class="line">    &#125;)</div><div class="line">    // 移除元素</div><div class="line">    .remove();</div><div class="line"></div><div class="line">  // exit集中节点的cycle元素尺寸变为0</div><div class="line">  nodeExit.select("circle").attr("r", 1e-6);</div><div class="line"></div><div class="line">  // exit集中节点的text元素可见度降为0</div><div class="line">  nodeExit.select("text").style("fill-opacity", 1e-6);</div><div class="line"></div><div class="line">  // ****************** 连接线部分操作 ***************************</div><div class="line"></div><div class="line">  // 更新数据</div><div class="line">  var link = svg.selectAll("path.link").data(links, function(d) &#123;</div><div class="line">    return d.id;</div><div class="line">  &#125;);</div><div class="line"></div><div class="line">  // 添加enter操作，添加类名为link的path元素</div><div class="line">  var linkEnter = link</div><div class="line">    .enter()</div><div class="line">    .insert("path", "g")</div><div class="line">    .attr("class", "link")</div><div class="line">    // 默认位置为当前父节点的位置</div><div class="line">    .attr("d", function(d) &#123;</div><div class="line">      var o = &#123; x: source.x0, y: source.y0 &#125;;</div><div class="line">      return diagonal(o, o);</div><div class="line">    &#125;);</div><div class="line"></div><div class="line">  // 获取update集</div><div class="line">  var linkUpdate = linkEnter.merge(link);</div><div class="line"></div><div class="line">  // 更新添加过渡动画</div><div class="line">  linkUpdate</div><div class="line">    .transition()</div><div class="line">    .duration(duration)</div><div class="line">    .attr("d", function(d) &#123;</div><div class="line">      return diagonal(d, d.parent);</div><div class="line">    &#125;);</div><div class="line"></div><div class="line">  // 获取exit集</div><div class="line">  var linkExit = link</div><div class="line">    .exit()</div><div class="line">    // 设置过渡动画</div><div class="line">    .transition()</div><div class="line">    .duration(duration)</div><div class="line">    .attr("d", function(d) &#123;</div><div class="line">      var o = &#123; x: source.x, y: source.y &#125;;</div><div class="line">      return diagonal(o, o);</div><div class="line">    &#125;)</div><div class="line">    // 移除link</div><div class="line">    .remove();</div><div class="line"></div><div class="line">  // 为动画过渡保存旧的位置</div><div class="line">  nodes.forEach(function(d) &#123;</div><div class="line">    d.x0 = d.x;</div><div class="line">    d.y0 = d.y;</div><div class="line">  &#125;);</div><div class="line"></div><div class="line">  // 添加贝塞尔曲线的path，衔接与父节点和子节点间</div><div class="line">  function diagonal(s, d) &#123;</div><div class="line">    path = `M $&#123;s.y&#125; $&#123;s.x&#125;</div><div class="line">            C $&#123;(s.y + d.y) / 2&#125; $&#123;s.x&#125;,</div><div class="line">              $&#123;(s.y + d.y) / 2&#125; $&#123;d.x&#125;,</div><div class="line">              $&#123;d.y&#125; $&#123;d.x&#125;`;</div><div class="line"></div><div class="line">    return path;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  // 当点击时，切换children，同时用_children来保存原子节点信息</div><div class="line">  function click(d) &#123;</div><div class="line">    if (d.children) &#123;</div><div class="line">      d._children = d.children;</div><div class="line">      d.children = null;</div><div class="line">    &#125; else &#123;</div><div class="line">      d.children = d._children;</div><div class="line">      d._children = null;</div><div class="line">    &#125;</div><div class="line">    update(d);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>以上的逻辑处理，无非是<code>初始化图表 -&gt; 获取数据 -&gt; 数据转换 -&gt; 元素操作/交互/动画 -&gt; 事件绑定 -&gt; 数据更新 -&gt; 元素更新</code>这样的流程吧。</p>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>这节主要分析了需求场景，接着找了个基本的 Tree Demo，然后结合上节讲过的 D3 知识，理解每个步骤的逻辑。<br>后面将结合实际需要的功能，一点点地说明实现的过程，以及将涉及到的 D3 更多的 API，甚至核心原理吧。</p>
  
		
		<p style="margin-top:50px;">
			查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a>
			<br>
			更欢迎来<a href="https://godbasin.github.io/front-end-playground">被删的前端游乐场</a>边撸猫边学前端噢
		</p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/D3小馒头/">D3小馒头</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2018/01/06/vue2-notes-10-access-route/" title="Vue2使用笔记10--路由鉴权">
  <strong>PREVIOUS:</strong><br/>
  <span>
  Vue2使用笔记10--路由鉴权</span>
</a>
</div>


<div class="next">
<a href="/2017/12/31/d3-tree-notes-1-svg-and-d3/"  title="D3.js-Tree实战笔记1--拜见D3.js">
 <strong>NEXT:</strong><br/> 
 <span>D3.js-Tree实战笔记1--拜见D3.js
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#基本场景"><span class="toc-number">1.</span> <span class="toc-text">基本场景</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Tree-图与服务"><span class="toc-number">1.1.</span> <span class="toc-text">Tree 图与服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#选择插件库"><span class="toc-number">1.2.</span> <span class="toc-text">选择插件库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#寻找-Demo"><span class="toc-number">1.3.</span> <span class="toc-text">寻找 Demo</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解读-Demo"><span class="toc-number">2.</span> <span class="toc-text">解读 Demo</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#整体逻辑"><span class="toc-number">2.1.</span> <span class="toc-text">整体逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#代码说明"><span class="toc-number">2.2.</span> <span class="toc-text">代码说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">2.3.</span> <span class="toc-text">结束语</span></a></li></ol></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/15/wxapp-latest-20191115/" title="小程序开发月刊第11期（20191115）">小程序开发月刊第11期（201911...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/10/change-log/" title="前端 CHANGELOG 生成指南">前端 CHANGELOG 生成指南...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/09/wxapp-global-data-behavior/" title="小程序的奇技淫巧 -- globalDataBehavior管理全局状态">小程序的奇技淫巧 -- global...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/15/wxapp-latest-20191015/" title="小程序开发月刊第十期（20191015）">小程序开发月刊第十期（2019101...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/13/about-front-end-3-growth/" title="前端这几年--3.关于成长和焦虑">前端这几年--3.关于成长和焦虑...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/09/16/wxapp-latest-20190916/" title="小程序开发月刊第九期（20190916）">小程序开发月刊第九期（2019091...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/08/15/wxapp-latest-20190815/" title="小程序开发月刊第八期（20190815）">小程序开发月刊第八期（2019081...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/27/front-end-playground/" title="被删的前端游乐场建成！">被删的前端游乐场建成！</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/21/vue-for-everyone-3/" title="9102全员学Vue--3.把页面拼成个Web应用">9102全员学Vue--3.把页面拼...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/15/wxapp-latest-20190715/" title="小程序开发月刊第七期（20190715）">小程序开发月刊第七期（2019071...</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">十月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">九月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>29</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>27</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/自动化甜筒/" title="自动化甜筒">自动化甜筒<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>73</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
